const te = new TextEncoder();
const td = new TextDecoder();

export const encode = (s) => te.encode(s);

export const decode = (arr) => {
  const ix = arr.findIndex(byte => byte === 0);
  const length = ix === -1 ? arr.length : ix;
  return td.decode(arr.subarray(0, length));
}

export const makeEnum = (...names) => {
  let obj = {};
  names.forEach((name, i) => {
    obj[name] = i;
  });
  return Object.freeze(obj);
};

export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
    return v !== null && v !== undefined;
}
